package com.ouzexi.finalexp

import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.os.Looper
import android.util.Log
import android.view.View
import android.widget.AdapterView
import android.widget.ListView
import android.widget.Toast
import com.google.gson.Gson
import com.ouzexi.finalexp.adapter.PositionAdapter
import com.ouzexi.finalexp.util.save
import com.ouzexi.finalexp.vo.Favor
import com.ouzexi.finalexp.vo.Position
import okhttp3.*
import java.io.IOException
import java.math.BigDecimal

class index : AppCompatActivity() {

    lateinit var token:String

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_index)

        token = save().getData(this,"token")

        val jid = intent.getStringExtra("jid")
        val jtypename = intent.getStringExtra("jtypename")

        save().saveData(this,"jid",jid.toString())
        save().saveData(this,"jtypename",jtypename.toString())

        getIntentPosition()

    }

    fun initView(result: String?) {
        val favor = Gson().fromJson(result, Favor::class.java)

        val jtypename = save().getData(this,"jtypename")

        if(favor.code!=2){
            // 到时可以传一个自增的id 然后跳转时根据id获取岗位详情信息
            val pos = ArrayList<Position>()

            runOnUiThread{
                for(i in favor.data.indices) {
                    pos.add(
                        Position(i,
                            favor.data[i].jname,
                            jtypename,
                            BigDecimal(favor.data[i].payLow*1000),
                            "00",
                            favor.data[i].demand,
                            favor.data[i].responsibility,
                            "东莞",
                            favor.data[i].period)
                    )
                }
                // 获取ListView组件
                val List = findViewById<ListView>(R.id.List)

                val adapter = PositionAdapter(this,R.layout.position_item,pos)
                List?.adapter  = adapter

                // 点击监听器 转到岗位详情
                List.onItemClickListener = AdapterView.OnItemClickListener { parent, view, position, id ->
                    Log.i("testClick","ClickListen")
                    val intent = Intent(this, addAdmire::class.java)
                    intent.putExtra("jname",favor.data[id.toInt()].jname)
                    intent.putExtra("jtype",jtypename)
                    intent.putExtra("jpay",favor.data[id.toInt()].payLow*1000)
                    intent.putExtra("jst","00")
                    intent.putExtra("jdemand",favor.data[id.toInt()].demand)
                    intent.putExtra("jresponsibility",favor.data[id.toInt()].responsibility)
                    intent.putExtra("jename",favor.data[id.toInt()].ename)
                    intent.putExtra("jperiod",favor.data[id.toInt()].period)
                    intent.putExtra("jid",favor.data[id.toInt()].id)
                    startActivity(intent)
                }
            }
        }
        else{
            Looper.prepare()
            Toast.makeText(this,"没有数据了", Toast.LENGTH_LONG).show()
            Looper.loop()
        }
    }

    private fun getIntentPosition() {

        val jid = save().getData(this,"jid")

        val urlAPI = "http://10.0.2.2:8080/jobs/job-position/getJobByJtype?jtype=${jid}"
        Log.i("minetoken", token)
        val request = Request.Builder()
            .url(urlAPI)
            .addHeader("token", token)
            .get()
            .build()

        val client = OkHttpClient()
        client.newCall(request).enqueue(object : Callback {
            override fun onResponse(call: Call, response: Response) {
                val result = response.body?.string()
                Log.i("test",result.toString())
                Log.i("testid",jid.toString())
                initView(result)

            }
            override fun onFailure(call: Call, e: IOException) {
                println("Failed request api :( " + e.message)
            }
        })

    }

    fun mine(view: View) {
        val intent = Intent(this, mine::class.java)
        startActivity(intent)
    }
}